//
//  DirectoryRequest.m
//  Cbchot
//
//  Created by 李雷川 on 2017/12/6.
//  Copyright © 2017年 王越. All rights reserved.
//

#import "DirectoryRequest.h"
#import "BookDetail.h"
#import "iTools.h"
#import <CBCReader/CBCReader.h>
@interface DirectoryRequest()<RequestBaseDelegate>

/**当前视频对象*/
@property (strong, nonatomic) BookDetail *bookDetail;

@end
@implementation DirectoryRequest

-(void)startRequest:(NSString*)bookID  success:(CAll_BACK) sucuessBack fail:(CAll_BACK) failBack
{
    self.successBack = sucuessBack;
    self.failBack = failBack;
    
    NSString* url = [NSString stringWithFormat:@"%@/api/book/get_directory/",[iTools ServerHost]];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
                         bookID,@"book_id",
                          @"0",@"start_index",
                          @"100",@"index_count",
                         nil];
    
    [self doPostRequest:url PostBody:dic Delegate:self];
}


/**解析数据*/
-(void)parserData:(NSDictionary *)dic
{
    NSLog(@"dic is:%@",dic);

    _bookDetail = [[BookDetail alloc]init];
    NSDictionary *headerInfo = [dic valueForKey:@"headerInfo"];
    NSArray *chapterList = [dic valueForKey:@"chapterList"];
    if(headerInfo != nil){
        _bookDetail.bookID = [headerInfo valueForKey:@"bookId"];
        _bookDetail.bookName = [headerInfo valueForKey:@"bookName"];
        _bookDetail.chapterTotalNum = [[headerInfo valueForKey:@"bookChapterTotalNum"] integerValue];
        NSString *jsonPath = [CBCReadTools getBookChapterJsonPath: _bookDetail.bookID];
        NSString *content = [self convertToJsonData:dic];
        [content writeToFile:jsonPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    }
    NSMutableArray *chapters = [[NSMutableArray alloc]initWithCapacity:_bookDetail.chapterTotalNum];
    [chapterList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSDictionary *chapterDic = obj;
        Chapter *chapter = [Chapter chapterWithDictionary:chapterDic];
        [chapters addObject:chapter];
    }];
    _bookDetail.chapters = chapters;
}

-(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}

/*联网解析完成*/
- (void)requestSuccess:(NSString*)message
{
    if(self.successBack){
        self.successBack(self.bookDetail);
    }
}

/*联网失败*/
- (void)requestFailed:(NSString *)errorMessage
{
    if(self.failBack){
        self.failBack(errorMessage);
    }
}

@end
